﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CreatureSoft.Hoot.Domain.DocumentManagent;
using CreatureSoft.Hoot.Data.Repositories;
using CreatureSoft.Hoot.Data.Infrastructure;

namespace CreatureSoft.Hoot.Service.DocumentManagent
{
    public class FolderService : IFolderService
    {
        private readonly IFolderRepository folderRepository;
        private readonly IUnitOfWork unitOfWork;

        public FolderService(IFolderRepository folderRepository, IUnitOfWork unitOfWork)
        {
            this.folderRepository = folderRepository;
            this.unitOfWork = unitOfWork;
        }  
       
        #region IFolderService Members

        public IList<Folder> GetFolders()
        {
            var folders = folderRepository.GetAll();
            return folders;
        }

        public IList<Folder> GetFoldersByParentId(int parentId)
        {
            
            if (parentId == 0)
            {
                var folders = folderRepository.GetMany(folder => folder.ParentFolder == null);
                return folders;
            }
            else
            {
                var folders = folderRepository.GetMany(folder => folder.ParentFolder != null && folder.ParentFolder.Id == parentId);
                return folders;
            }
        }

        public Folder GetFolder(int id)
        {
            var folder = folderRepository.GetById(id);
            return folder;
        }

        public void CreateFolder(Folder folder)
        {
            folderRepository.Add(folder);
            SaveFolder();
        }

        public void DeleteFolder(int id)
        {
            var folder = folderRepository.GetById(id);
            folderRepository.Delete(folder);
            SaveFolder();
        }

        public void SaveFolder()
        {
            unitOfWork.Commit();
        }

        #endregion




        
    }
}
